package com.example.backend.controller;

import com.example.backend.dto.ApiResponse;
import com.example.backend.service.StatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 统计控制器
 * 提供数据统计API
 */
@RestController
@RequestMapping("/api/statistics")
@Tag(name = "数据统计", description = "系统数据统计相关接口")
@SecurityRequirement(name = "Bearer Authentication")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @GetMapping("/overview")
    @Operation(summary = "获取系统概览", description = "获取系统整体统计信息")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getSystemOverview() {
        try {
            Map<String, Object> overview = statisticsService.getSystemOverview();
            return ResponseEntity.ok(ApiResponse.success("获取系统概览成功", overview));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取系统概览失败: " + e.getMessage()));
        }
    }

    @GetMapping("/tickets")
    @Operation(summary = "获取工单统计", description = "获取工单相关统计信息")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTicketStatistics() {
        try {
            Map<String, Object> stats = statisticsService.getTicketStatistics();
            return ResponseEntity.ok(ApiResponse.success("获取工单统计成功", stats));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取工单统计失败: " + e.getMessage()));
        }
    }

    @GetMapping("/feedbacks")
    @Operation(summary = "获取反馈统计", description = "获取反馈相关统计信息")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getFeedbackStatistics() {
        try {
            Map<String, Object> stats = statisticsService.getFeedbackStatistics();
            return ResponseEntity.ok(ApiResponse.success("获取反馈统计成功", stats));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取反馈统计失败: " + e.getMessage()));
        }
    }

    @GetMapping("/users")
    @Operation(summary = "获取用户统计", description = "获取用户相关统计信息")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getUserStatistics() {
        try {
            Map<String, Object> stats = statisticsService.getUserStatistics();
            return ResponseEntity.ok(ApiResponse.success("获取用户统计成功", stats));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取用户统计失败: " + e.getMessage()));
        }
    }

    @GetMapping("/charts/ticket-trend")
    @Operation(summary = "获取工单趋势图表数据", description = "获取工单趋势分析图表所需的数据")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTicketTrendChartData() {
        try {
            Map<String, Object> chartData = statisticsService.getTicketTrendChartData();
            return ResponseEntity.ok(ApiResponse.success("获取工单趋势图表数据成功", chartData));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取工单趋势图表数据失败: " + e.getMessage()));
        }
    }

    @GetMapping("/charts/ticket-status")
    @Operation(summary = "获取工单状态分布图表数据", description = "获取工单状态分布饼图所需的数据")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTicketStatusChartData() {
        try {
            Map<String, Object> chartData = statisticsService.getTicketStatusChartData();
            return ResponseEntity.ok(ApiResponse.success("获取工单状态分布图表数据成功", chartData));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取工单状态分布图表数据失败: " + e.getMessage()));
        }
    }
}
